package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ams.domain.AmsAssetAllocationDetail;
import com.ruoyi.ams.service.IAmsAssetAllocationDetailService;
import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 资产调拨单明细Controller
 * 
 * @author yfkj
 * @date 2022-05-16
 */
@RestController
@RequestMapping("/ams/allocationDetail")
public class AmsAssetAllocationDetailController extends BaseController
{
    @Autowired
    private IAmsAssetAllocationDetailService amsAssetAllocationDetailService;

    /**
     * 查询资产调拨单明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:list")
    //@ApiOperation(value = "获取资产调拨单明细列表")
    @GetMapping("/getAmsAssetAllocationDetailList")
    public TableDataInfo getAmsAssetAllocationDetailList(AmsAssetAllocationDetail amsAssetAllocationDetail)
    {
        startPage();
        List<AmsAssetAllocationDetail> list = amsAssetAllocationDetailService.selectAmsAssetAllocationDetailList(amsAssetAllocationDetail);
        return getDataTable(list);
    }


    /**
     * 获取资产调拨单明细详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:query")
    @ApiOperation(value = "获取资产调拨单明细详情")
    @GetMapping(value = "/getAmsAssetAllocationDetailById")
    public AjaxResult getAmsAssetAllocationDetailById(@RequestParam("allocationId") Long allocationId)
    {
        return AjaxResult.success(amsAssetAllocationDetailService
                .getOne(new QueryWrapper<AmsAssetAllocationDetail>()
                        .eq(AmsAssetAllocationDetail.ALLOCATION_ID, allocationId), false));
    }

    /**
     * 新增资产调拨单明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:add")
    @Log(title = "资产调拨单明细", businessType = BusinessType.INSERT)
    //@ApiOperation(value = "新增资产调拨单明细")
    @PostMapping(value = "/addAmsAssetAllocationDetail")
    public AjaxResult addAmsAssetAllocationDetail(@RequestBody AmsAssetAllocationDetail amsAssetAllocationDetail)
    {
        return toAjax(amsAssetAllocationDetailService.save(amsAssetAllocationDetail));
    }

    /**
     * 修改资产调拨单明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:edit")
    @Log(title = "资产调拨单明细", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改资产调拨单明细")
    @PostMapping(value = "/editAmsAssetAllocationDetail")
    public AjaxResult editAmsAssetAllocationDetail(@RequestBody AmsAssetAllocationDetail amsAssetAllocationDetail)
    {
        return toAjax(amsAssetAllocationDetailService.updateById(amsAssetAllocationDetail));
    }

    /**
     * 删除资产调拨单明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:remove")
    @Log(title = "资产调拨单明细", businessType = BusinessType.DELETE)
	@GetMapping("/removeAmsAssetAllocationDetails")
    public AjaxResult removeAmsAssetAllocationDetails(@RequestParam Long[] allocationDetailIds)
    {
        return toAjax(amsAssetAllocationDetailService.deleteAmsAssetAllocationDetailByAllocationDetailIds(allocationDetailIds));
    }
    @GetMapping("/getAmsAssetAllocationDetailNamesForSelect")
    //@ApiOperation(value = "获取资产调拨单明细名称列表用于下拉搜索")
    public AjaxResult getAmsAssetAllocationDetailNamesForSelect()
    {
        List<AmsAssetAllocationDetail> list = amsAssetAllocationDetailService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (AmsAssetAllocationDetail amsAssetAllocationDetail: list ) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 导出资产调拨单明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:export")
    @Log(title = "资产调拨单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmsAssetAllocationDetail")
    public String exportAmsAssetAllocationDetail(HttpServletResponse response, AmsAssetAllocationDetail amsAssetAllocationDetail) throws IOException
    {
        List<AmsAssetAllocationDetail> list = amsAssetAllocationDetailService.selectAmsAssetAllocationDetailList(amsAssetAllocationDetail);
        ExcelUtil<AmsAssetAllocationDetail> util = new ExcelUtil<AmsAssetAllocationDetail>(AmsAssetAllocationDetail.class);
        util.exportExcel(response, list, "资产调拨单明细数据");
        return "";
    }

}
